var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxNotificationModel = function (obj) {
    YjxBaseModel.call(this,obj);
    /*  `ntfy_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '消息id',
      `ntfy_type` int(3) NOT NULL DEFAULT '0' COMMENT '消息分类: 11 一键秀APP 开机广告, 12 一键秀APP 轮播图, 13 一键秀APP 发现轮播图,  21: 一键秀 web 轮播图',
      `image` varchar(1000) DEFAULT '' COMMENT '开机广告/轮播图 对应的图片资源链接, 如果需要上传 gif, 则图片链接后缀需要为 ".gif"',
      `url` varchar(1000) DEFAULT '' COMMENT '开机广告点击跳转的链接/轮播图点击跳转的资源链接, "http"开头的合法 url, 如需扩展则修改链接头, 如: "app://"',
      `start_time` datetime NOT NULL COMMENT '开机广告/轮播图, 有效期开始时间',
      `end_time` datetime DEFAULT NULL COMMENT '开机广告/轮播图, 有效期截止时间',
      `disp_type` int(3) NOT NULL DEFAULT '0' COMMENT '显示分类, APP 开机广告专用, 1. APP 启动显示, 2. app 进入后台显示, 3 app 启动和进入后台均显示''',
      `disp_dur` int(3) NOT NULL DEFAULT '0' COMMENT '显示时长, APP 开机广告专用, 开机广告展示的默认时长',
      `chk_freq` bigint(10) DEFAULT '0' COMMENT '两此开机广告展示间隔的最小时长, 单位: 秒, 0秒标记每次都显示',
      `title` varchar(200) DEFAULT NULL COMMENT '预留字段, 暂不处理',
      `message` varchar(1000) DEFAULT NULL COMMENT '预留字段, 暂不处理',
      `encrypt` int(1) NOT NULL DEFAULT '0' COMMENT '预留字段, 暂不处理',
      `ver` varchar(20) DEFAULT NULL COMMENT '预留字段, 暂不处理',
      `order` int(3) NOT NULL DEFAULT '0' COMMENT '排序序号(轮播图排序序号, 开机广告同时只能存在一个)',
      `create_time` datetime NOT NULL,
      `update_time` datetime NOT NULL,
      `update_by` varchar(50) DEFAULT NULL,
      `del_flg` int(1) NOT NULL DEFAULT '0',*/
};

YjxNotificationModel.prototype = new YjxBaseModel();

// 取得表名
YjxNotificationModel.prototype.getTableName = function(){
    return 'itb_notification';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxNotificationModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
};

/**
 * 获取 APP 开屏广告信息
 *
 * @author 李强
 * @date   2017-11-16
 *
 * @param screenW 需要的宽
 * @param screenH 需要的高
 * @param appVer  版本号
 * @param callback 回调
 * */
YjxNotificationModel.prototype.getAppAdInfo_v1_0 = function (screenW,screenH,appVer,callback) {
    pool.pool.getConnection(function(connectErr, connection) {
        if (connectErr) {
            console.log('YjxNotificationModel.getNotifications Err:'+connectErr);
            return callback(connectErr, null);
        }
        var sql =
            " SELECT n.url AS redirectUrl, " + // 跳转链接, 暂不处理
            "        n.image AS displayUrl, " +
            "        1 AS kind, " + // 类型 暂时只有1 标记纯图展示
            "        n.disp_dur AS displayTime, " + // 展示时长, 数据库暂不记录, 默认为3: 单位: 秒
            "        n.chk_freq AS displayInterval, " + // 两次开屏广告展示的最小间隔, 单位: 秒
            "        n.disp_type AS displayType" +
            " FROM yjx.yjx_notification n " +
            " WHERE n.ntfy_type=11 " +              // 指定开机广告类型
            "   AND SYSDATE() < n.end_time " +
            "   AND SYSDATE() > n.start_time " +
            "   AND n.del_flg = 0 " +
            " ORDER BY n.start_time DESC ";
        console.log(sql);
        connection.query(sql, function(queryErr, result) {
            // And done with the connection.
            connection.release();

            if(queryErr || result.length <= 0) {
                return callback(queryErr || '暂无开屏广告图信息', null);
            }
            return callback(queryErr, result[0]);
        });
    });
};


/**
 * 获取 APP banner 图信息
 *
 * @author 王俊
 * @date 2018-01-05
 *
 * @param callback 回调函数
 *          error: 查询失败
 *          result: 轮播图列表
 */
YjxNotificationModel.prototype.getAppBanner_v1_1 = function (callback) {
    pool.pool.getConnection(function(connectErr, connection) {
        if (connectErr) {
            console.log('YjxNotificationModel.getNotifications Err:'+connectErr);
            return callback(connectErr, null);
        }
        var sql =   ' SELECT n.ntfy_id AS id, ' +
                    '        n.image , ' +
                    '        n.url ' +
                    ' FROM yjx.yjx_notification n ' +
                    ' WHERE n.ntfy_type = 12 ' +            // 指定 app 轮播图
                    '   AND SYSDATE() > n.start_time ' +
                    '   AND SYSDATE() < n.end_time ' +
                    '   AND n.del_flg = 0 ' +
                    ' ORDER BY n.order ';
        console.log(sql);
        connection.query(sql, function(queryErr, result) {
            // And done with the connection.
            connection.release();
            return callback(queryErr, result);
        });
    });
};


/**
 * 获取 Web banner 图信息
 *
 * @author 毛凯
 * @date 2018-2-1
 *
 * @param callback 回调函数
 *          error: 查询失败
 *          result: 轮播图列表
 */
YjxNotificationModel.prototype.getWebBanner_v1_1 = function (callback) {
    pool.pool.getConnection(function(connectErr, connection) {
        if (connectErr) {
            console.log('YjxNotificationModel.getNotifications Err:'+connectErr);
            return callback(connectErr, null);
        }
        var sql =   ' SELECT n.ntfy_id AS id, ' +
            '        n.image , ' +
            '        n.url ' +
            ' FROM yjx.yjx_notification n ' +
            ' WHERE n.ntfy_type = 21 ' +            // 指定 app 轮播图
            '   AND SYSDATE() > n.start_time ' +
            '   AND SYSDATE() < n.end_time ' +
            '   AND n.del_flg = 0 ' +
            ' ORDER BY n.order ';
        console.log(sql);
        connection.query(sql, function(queryErr, result) {
            // And done with the connection.
            connection.release();
            return callback(queryErr, result);
        });
    });
};



/**
 * 获取 弹窗广告 图信息
 *
 * @author 毛凯
 * @date 2018-7-23
 *
 * @param callback 回调函数
 *          error: 查询失败
 *          result: 轮播图列表
 */
YjxNotificationModel.prototype.getWindowInfo = function (callback) {
    pool.pool.getConnection(function(connectErr, connection) {
        if (connectErr) {
            console.log('YjxNotificationModel.getNotifications Err:'+connectErr);
            return callback(connectErr, null);
        }
        var sql = "	SELECT	" +
            "	  n.ntfy_id AS id,	" +
            "	  n.embed_url AS contentUrl,	" +
            "	  n.chk_freq AS displayInterval,	" +
            "	  n.disp_type AS displayType,	" +
            "	  n.w AS windowW,	" +
            "	  n.h AS windowH	" +
            "	FROM	" +
            "	  yjx.yjx_notification n	" +
            "	WHERE n.ntfy_type = 14	" +
            "	  AND SYSDATE() < n.end_time	" +
            "	  AND SYSDATE() > n.start_time	" +
            "	  AND n.del_flg = 0;	" ;
            console.log(sql);
        connection.query(sql, function(queryErr, result) {
            // And done with the connection.
            connection.release();
            return callback(queryErr, result);
        });
    });
};




module.exports = YjxNotificationModel;